﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Baosight.ZNH.ZMCommon.ChildWindows;
using Baosight.ZNH.ZMCommon;
using Baosight.ZNH.ZMCommon.Validators;
using Baosight.Platform.Core;
using Baosight.Platform.Core.ServiceModel;
using Baosight.Platform.Core.UI.Controls;
using Telerik.Windows.Controls;

namespace Baosight.ZNH.ZMCM.UI
{
    public partial class ZMCMF1 : EFViewCmes
    {
        public ZMCMF1()
        {
            InitializeComponent();
        }

        /// <summary>
        /// 点击确定按钮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void CommitClick(object sender, RoutedEventArgs e)
        {
            //按钮不可用，防止双击
            Button button = sender as Button;
            button.IsEnabled = false;
            TempButton = button;

            int count = this.grid.CheckedEIRows.Count;
            if (count > 0)
            {
                List<string> list = new List<string>();
                if (!this.grid.Validate(DataRange.CheckedItems,out list))
                {
                    this.ValidateMessagePopup();//弹出提示信息
                    this.TempButtonOpen(true);//确定按钮生效
                    return;
                }
                else
                {
                    string msg = "";
                    foreach (var record in this.grid.CheckedEIRows)
                    {
                        
                        var fdActualNum = record["fdActualNum"];
                        var fdSum = record["fdSum"];
                        var fdRecevieSum = record["fdRecevieSum"];
                        var fdMaterialName = record["fdMaterialName"];
                        
                        //如果输入的实收数量大于采购数量减去已到货数量，弹出确认框
                        if (Double.Parse(fdActualNum.ToString()) > Double.Parse(fdSum.ToString()) - Double.Parse(fdRecevieSum.ToString()))
                        {
                            msg += fdMaterialName + "物资、";
                        }                                                
                    }
                    if (msg.Equals(""))
                    {
                        EIInfo eiInfo = new EIInfo();
                        eiInfo.Set("service", "ZMCMF0");
                        eiInfo.Set("method", "insert");

                        //主表新增block
                        this.BaseBlock(eiInfo);

                        //列表新增下一步block
                        this.SubBlock(eiInfo);

                        HttpServiceAgent agent = new HttpServiceAgent();
                        agent.CallService(eiInfo, OnDoWorkCompleted);
                    }
                    else
                    {
                        //弹出确认框
                        DialogParameters param = this.ModelDialogParameters("Confirm", msg.Substring(0, msg.Length - 1) + "实收数量与已到货数量的总和大于采购数量，请确定是否进行到货登记？");
                        param.Closed = ConfirmClosed;
                        EFWindow.Confirm(param);
                    }
                }
            }
            else
            {
                this.OpenEFWindowAlert("请选择到货物资的记录！", true);
                return;
            }

        }

        /// <summary>
        /// 弹出确认框后，若取消不提交后台，确认提交数据库
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ConfirmClosed(object sender, WindowClosedEventArgs e)
        {
            if (e.DialogResult == null || !e.DialogResult.Value)
            {
                this.TempButtonOpen(true);//取消后，删除按钮生效
                return;
            }

            if (e.DialogResult.Value)
            {
                EIInfo eiInfo = new EIInfo();
                eiInfo.Set("service", "ZMCMF0");
                eiInfo.Set("method", "insert");

                //主表新增block
                this.BaseBlock(eiInfo);

                //列表新增下一步block
                this.SubBlock(eiInfo);

                HttpServiceAgent agent = new HttpServiceAgent();
                agent.CallService(eiInfo, OnDoWorkCompleted);
            }
        }

        /// <summary>
        /// 新增后的回调方法，用于提醒用户，如需调整计划价，请进入调价模块
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void OnDoWorkCompleted(object sender, DoWorkCompletedEventArgs e)
        {
            EIInfo inInfo = e.EIInfo;
            
            if (inInfo.Status == -1)
            {
                string str = inInfo.GetMsg();
                this.OpenEFWindowAlert(str, true);//弹出提示信息
                return;
            }
            else if (!inInfo.GetDetailMsg().Equals(""))
            {
                this.OpenEFWindowAlert(inInfo.GetDetailMsg(), true);
                this.Shell.LoadView("ZMCMF0");
                UpdateHomePageData(e.EIInfo);
            }
            else
            {
                this.Shell.LoadView("ZMCMF0");
                UpdateHomePageData(e.EIInfo);
            }
        }

        /// <summary>
        /// 点击返回按钮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void CancelClick(object sender, RoutedEventArgs e)
        {
            this.Shell.LoadView("ZMCMF0");
        }


        /// <summary>
        /// 点击查看所有到货记录按钮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void LookupClick(object sender, RoutedEventArgs e)
        {

            this.Shell.LoadView("ZMCMF2");
        }

        /// <summary>
        /// 将新增页面中的录入信息组织成一个block
        /// </summary>
        /// <param name="eiInfo"></param>
        private void BaseBlock(EIInfo eiInfo)
        {
            EIBlock baseBlock = new EIBlock("baseBlock");
            EIColumn column = new EIColumn();
            column.SetName("fdDate");
            baseBlock.AddColumn(column);

            column = new EIColumn();
            column.SetName("fdDistribute");
            baseBlock.AddColumn(column);

            column = new EIColumn();
            column.SetName("fdReason");
            baseBlock.AddColumn(column);

            column = new EIColumn();
            column.SetName("fdSum");
            baseBlock.AddColumn(column);

            column = new EIColumn();
            column.SetName("fdSupplyer");
            baseBlock.AddColumn(column);

            column = new EIColumn();
            column.SetName("fdSupplyName");
            baseBlock.AddColumn(column);

            column = new EIColumn();
            column.SetName("fdRemark");
            baseBlock.AddColumn(column);

            EIRow baseRow = new EIRow();
            baseRow.Add("fdDate", this.EIDataContext.GetAttribute("fdDate"));
            baseRow.Add("fdDistribute", this.EIDataContext.GetAttribute("fdDistribute"));
            baseRow.Add("fdReason", this.EIDataContext.GetAttribute("fdReason"));
            baseRow.Add("fdSum", this.EIDataContext.GetAttribute("fdSum"));
            baseRow.Add("fdSupplyer", this.EIDataContext.GetAttribute("fdSupplyer"));
            baseRow.Add("fdSupplyName", this.EIDataContext.GetAttribute("fdSupplyName"));
            baseRow.Add("fdRemark", this.EIDataContext.GetAttribute("fdRemark"));

            baseBlock.AddRow(baseRow);
            eiInfo.AddBlock(baseBlock);
        }

        /// <summary>
        /// 将列表中选中记录组织成一个block
        /// </summary>
        /// <param name="eiInfo"></param>
        private void SubBlock(EIInfo eiInfo) 
        {
            EIBlock subBlock = new EIBlock("subBlock");
            subBlock.SetColumns(this.grid.EIBlock.GetColumns());
            subBlock.SetRows(this.grid.CheckedEIRows);
            eiInfo.AddBlock(subBlock);
        }

    }
}
